//
//  QQForgetPassWViewController.m
//  ScrollViews
//
//  Created by QQImac on 2018/4/2.
//  Copyright © 2018年 Mr.Lee. All rights reserved.
//

#import "QQForgetPassWViewController.h"


@interface QQForgetPassWViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneNumberT;
@property (weak, nonatomic) IBOutlet UITextField *codeNumberT;
@property (weak, nonatomic) IBOutlet UITextField *passWorldNewT;
@property (weak, nonatomic) IBOutlet UITextField *passWorldNewTT;

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *forgetBtn;



@end

@implementation QQForgetPassWViewController
{
    NSTimer *timer;
    NSInteger iSmsValidateSeconds;
    NSInteger iSmsValidateSecondsDefault;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"忘记密码";
    iSmsValidateSecondsDefault=60;
    
    [_passWorldNewT setSecureTextEntry:YES];
    [_passWorldNewTT setSecureTextEntry:YES];
   
    // 添加监听
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFiledEditChanged:)
                                                name:@"UITextFieldTextDidChangeNotification"
                                              object:_phoneNumberT];
    if (_type == NO) return;
    self.title = @"找回密码";
    _phoneNumberT.text = kQQUserInfoManger.mobile;
    _phoneNumberT.userInteractionEnabled = NO;
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}


- (IBAction)comfirmClick:(id)sender {
    
    if ((_phoneNumberT.text.length ==0)||(_codeNumberT.text.length==0)||(_passWorldNewT.text.length==0)||(_passWorldNewTT.text.length==0)) {
        [[XNProgressHUD shared] showErrorWithTitle:@"请输入完整信息"];
    }
    
    if (_phoneNumberT.text.length != 11) {
        [[XNProgressHUD shared] showErrorWithTitle:@"请注意手机号格式"];
        return;
    }

    if (_codeNumberT.text.length != 6) {
        [[XNProgressHUD shared] showErrorWithTitle:@"请输入验证码"];
        return;
    }
    if ((_passWorldNewT.text.length <3)||(_passWorldNewT.text.length >16)) {
        
        [[XNProgressHUD shared] showErrorWithTitle:@"请注意密码格式"];
        return;
    }
    if (![_passWorldNewT.text isEqualToString:_passWorldNewTT.text]) {
        [[XNProgressHUD shared] showErrorWithTitle:@"两次输入的密码不一致"];
        return;
    }
    NSDictionary *dic = @{
                           @"phoneNum":_phoneNumberT.text,
                           @"password":_passWorldNewT.text,
                           @"repeatPwd":_passWorldNewTT.text,
                           @"data":_codeNumberT.text
                           };
    [[LMJRequestManager sharedManager] POST:KBaseURL(@"user/updatepwd")  parameters:dic completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            [[XNProgressHUD shared] showWithTitle:response.responseModel.message];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

- (IBAction)getTheComfirmNumber:(UIButton *)sender {
    // 测试方便去掉手机号验证，
    if (_phoneNumberT.text.length != 11) {
        [[XNProgressHUD shared] showErrorWithTitle:@"手机号码格式错误"];
        return;
    }
    
    NSDictionary *parameters = @{@"phoneNum": _phoneNumberT.text,
                                 @"type": @3};

    [[LMJRequestManager sharedManager] POST:KBaseURL(@"user/sendmsg/free")  parameters:parameters completion:^(LMJBaseResponse *response) {
        if (response.responseModel.status == 1) {
            [[XNProgressHUD shared] showWithTitle:response.responseModel.message];
            [Utility startTime:sender];
        }
    }];
}



-(void)textFiledEditChanged:(NSNotification *)obj{
    UITextField *textField = (UITextField *)obj.object;
    
    NSString *toBeString = textField.text;
    NSString *lang = [[UITextInputMode currentInputMode] primaryLanguage]; // 键盘输入模式
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入，包括简体拼音，健体五笔，简体手写
        UITextRange *selectedRange = [textField markedTextRange];
        //获取高亮部分
        UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
        // 没有高亮选择的字，则对已输入的文字进行字数统计和限制
        if (!position) {
            if (toBeString.length > 11) {
                textField.text = [toBeString substringToIndex:11];
            }
        }
        // 有高亮选择的字符串，则暂不对文字进行统计和限制
        else{
            
        }
    }
    // 中文输入法以外的直接对其统计限制即可，不考虑其他语种情况
    else{
        if (toBeString.length > 11) {
            textField.text = [toBeString substringToIndex:11];
        }
    }
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self
                                                   name:@"UITextFieldTextDidChangeNotification"
                                                 object:_phoneNumberT];
}

@end
